package cn.SiFuLan.dao.impl;

import cn.SiFuLan.dao.IskuDao;
import cn.SiFuLan.entity.skuEntity;
import cn.SiFuLan.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class skuDaoImapl extends BaseDao implements IskuDao {

    @Override
    public List<skuEntity> all(int spuid) {
        String sql="SELECT Sku_id,Spu_id,title,color,specification,prive,valid,AddTime,UpdateTime FROM sku WHERE Spu_id = ? order by Sku_id asc  ";
        List<skuEntity> list=new ArrayList<>();
        List<Object> sz=new ArrayList<>();
        sz.add(spuid);
        ResultSet rs= executeQuery(sql,sz);
        while (true) {
            try {
                if (!rs.next()) break;
                skuEntity sku=new skuEntity();
                sku.setSku_id(rs.getInt("Sku_id"));
                sku.setSpu_id(rs.getInt("Spu_id"));
                sku.setTitle(rs.getString("title"));
                sku.setColor(rs.getString("color"));
                sku.setSpecification(rs.getString("specification"));
                sku.setPrive(rs.getString("prive"));
                sku.setValid(rs.getInt("valid"));
                sku.setAddTime(rs.getDate("AddTime"));
                sku.setUpdateTime(rs.getDate("UpdateTime"));
                list.add(sku);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        try {
            CloseAll(rs.getStatement().getConnection(),rs.getStatement(),rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<skuEntity> aall(skuEntity sku) {
        String sql="SELECT Sku_id,Spu_id,title,color,specification,prive,valid,AddTime,UpdateTime FROM sku WHERE Spu_id = ?   ";
        List<skuEntity> list=new ArrayList<>();
        List<Object> sz=new ArrayList<>();
        sz.add(sku.getSpu_id());
        if(sku!=null && !sku.equals("")){
            if(sku.getTitle()!=null && !sku.equals("")){
                sql+=" and title like'%"+sku.getTitle()+"%'";
            }
            if(sku.getColor()!=null && !sku.equals("")){
                sql+=" and color like'%"+sku.getColor()+"%'";
            }
        }

        ResultSet rs= executeQuery(sql,sz);
        while (true) {
            try {
                if (!rs.next()) break;
                skuEntity s=new skuEntity();
                s.setSku_id(rs.getInt("Sku_id"));
                s.setSpu_id(rs.getInt("Spu_id"));
                s.setTitle(rs.getString("title"));
                s.setColor(rs.getString("color"));
                s.setSpecification(rs.getString("specification"));
                s.setPrive(rs.getString("prive"));
                s.setValid(rs.getInt("valid"));
                s.setAddTime(rs.getDate("AddTime"));
                s.setUpdateTime(rs.getDate("UpdateTime"));
                list.add(s);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        try {
            CloseAll(rs.getStatement().getConnection(),rs.getStatement(),rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public skuEntity getall(int skuid) {
        String sql="SELECT Sku_id,Spu_id,title,color,specification,prive,valid,AddTime,UpdateTime FROM sku where Sku_id = ? ";
        skuEntity sku=new skuEntity();
        List<Object> list=new ArrayList<>();
        list.add(skuid);
        ResultSet rs= executeQuery(sql,list);
        while (true) {
            try {
                if (!rs.next()) break;
                sku.setSku_id(rs.getInt("Sku_id"));
                sku.setSpu_id(rs.getInt("Spu_id"));
                sku.setTitle(rs.getString("title"));
                sku.setColor(rs.getString("color"));
                sku.setSpecification(rs.getString("specification"));
                sku.setPrive(rs.getString("prive"));
                sku.setValid(rs.getInt("valid"));
                sku.setAddTime(rs.getDate("AddTime"));
                sku.setUpdateTime(rs.getDate("UpdateTime"));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        try {
            CloseAll(rs.getStatement().getConnection(),rs.getStatement(),rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return sku;
    }

    @Override
    public boolean update(skuEntity sku) {
        String sql="UPDATE sku SET title=?,color=?,specification=?,prive=?,valid=?,UpdateTime=? WHERE Sku_id=?";
        List<Object> sz=new ArrayList<>();
        sz.add(sku.getTitle());
        sz.add(sku.getColor());
        sz.add(sku.getSpecification());
        sz.add(sku.getPrive());
        sz.add(sku.getValid());
        sz.add(sku.getUpdateTime());
        sz.add(sku.getSku_id());
        return executeUpdate(sql,sz);
    }

    @Override
    public boolean add(skuEntity skuEntity) {
        String sql= "INSERT INTO sku(Spu_id,title,color,specification,prive,valid,AddTime,UpdateTime) VALUES (?,?,?,?,?,?,?,?)";
        List<Object> sz=new ArrayList<>();
        sz.add(skuEntity.getSpu_id());
        sz.add(skuEntity.getTitle());
        sz.add(skuEntity.getColor());
        sz.add(skuEntity.getSpecification());
        sz.add(skuEntity.getPrive());
        sz.add(skuEntity.getValid());
        sz.add(skuEntity.getAddTime());
        sz.add(skuEntity.getUpdateTime());
        return executeUpdate(sql,sz);
    }
}
